package com.l024.loanthief.views.homePage;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.l024.loanthief.R;
import com.l024.loanthief.adapter.CommonAdapter;
import com.l024.loanthief.adapter.CommonViewHolder;
import com.l024.loanthief.base.activity.BaseFragment;
import com.l024.loanthief.constant.APPConstant;
import com.l024.loanthief.databinding.FragmentHomeBinding;
import com.l024.loanthief.entity.DeviceEntity;
import com.l024.loanthief.entity.UserEntity;
import com.l024.loanthief.eventbus.ChatMessageEvent;
import com.l024.loanthief.views.chat.ChatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment {

    private FragmentHomeBinding binding;
    private CommonAdapter<UserEntity> userAdapter;
    private List<UserEntity> users = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_home,container,false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //扫描
        EventBus.getDefault().post(new ChatMessageEvent(APPConstant.SCAN_LIST,"",""));
        init();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(ChatMessageEvent event) {
        //处理逻辑
        if(event!=null){
            switch (event.getCode()){
                case APPConstant.DELECT_DEVICT_LIST:{
                    if(users!=null&&users.size()>0){
                        users.clear();
                        userAdapter.notifyDataSetChanged();
                    }
                    break;
                }
                case APPConstant.ADD_CLIENT:{
                    UserEntity userEntity = (UserEntity)event.getData();
                    if(userEntity!=null){
                        if(!users.contains(userEntity)){
                            users.add(userEntity);
                            userAdapter.notifyDataSetChanged();
                        }
                    }

                    break;
                }
                case APPConstant.CONNECT_CODE:{
                    //文本消息
                    DeviceEntity deviceEntity = (DeviceEntity)event.getData();
                    if(deviceEntity!=null){
                        UserEntity userEntity = new UserEntity();
                        userEntity.setIp(deviceEntity.getIp());
                        userEntity.setName(deviceEntity.getPhoneName());
                        userEntity.setIcon(deviceEntity.getImgId());
                        if(!users.contains(userEntity)){
                            users.add(userEntity);
                        }
                        if(userAdapter!=null){
                            userAdapter.notifyDataSetChanged();
                        }

                    }
                    break;
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().removeAllStickyEvents();
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onStart() {
        super.onStart();
        binding.rvRadar.startScan();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onStop() {
        super.onStop();
        binding.rvRadar.stopScan();
    }

    /**
     * 初始化数据
     */
    private void init() {
        users.add(new UserEntity(0,"局域网列表","",0,1));

        userAdapter = new CommonAdapter<UserEntity>(users, new CommonAdapter.OnMoreBindDataListener<UserEntity>() {
            @Override
            public int getItemType(int position) {
                return users.get(position).getType();
            }

            @Override
            public void onBindViewHolder(UserEntity model, CommonViewHolder viewHolder, int type, int position) {
                if(type==0){
                    //内容
                    viewHolder.setText(R.id.tv_name,users.get(position).getName());
                    viewHolder.setText(R.id.tv_ip,users.get(position).getIp());
                    viewHolder.getView(R.id.rl_item).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(getActivity(),ChatActivity.class);
                            intent.putExtra("deviceInfo", JSON.toJSONString(model));
                            startActivity(intent);
                        }
                    });
                }else{
                    //标题
                    viewHolder.setText(R.id.tv_title,users.get(position).getName());
                }
            }

            @Override
            public int getLayoutId(int type) {
               if(type==0){
                   return R.layout.item_home_user;
               }else{
                   return R.layout.item_home_title;
               }
            }
        });
        binding.rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.rvList.setAdapter(userAdapter);
        //scan();
    }

}
